package com.skyhub.controller;

import cn.hutool.json.JSONUtil;
import com.skyhub.common.domain.dto.UploadFormDto;
import com.skyhub.common.domain.pojo.Article;
import com.skyhub.common.domain.pojo.ArticleSeries;
import com.skyhub.common.domain.pojo.Result;
import com.skyhub.service.UploadService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/upload")
@RequiredArgsConstructor
@CrossOrigin
public class UploadController{

    private final UploadService uploadService;

    @PostMapping("/series")
    public Result series(@RequestBody UploadFormDto uploadFormDto){

        List<ArticleSeries>series=uploadService.series(uploadFormDto);

        if(!JSONUtil.isNull(series)){
            return Result.success("获取成功",series);
        }

        return Result.error("获取失败",null);
    }
    @PutMapping("/addSeries")
    public Result addSeries(@RequestBody UploadFormDto uploadFormDto){
        uploadService.addSeries(uploadFormDto);
        return Result.success("添加成功","你的系列已经添加成功，可以到主页查看了");
    }

    @PutMapping("/insertIntoArticles/{userId}")
    public Result insertIntoArticles(@RequestBody Article article,@PathVariable("userId")Integer userId){

        uploadService.insertIntoArticles(article,userId);

        return Result.success("上传成功","文章添加成功,3秒后退出本页面");
    }
}
